<?php

namespace App\Http\Controllers\H5\Shop;

use App\Http\Controllers\Controller;
use App\Model\Address;
use Illuminate\Http\Request;

class AddressController extends Controller
{
    /**
     * 地址列表
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function index(Request $request, Address $address)
    {
        $uid = $request->uid;
        $address = $address->getAddressList($uid);
        return ApiResponse($address);
    }

    /**
     * 添加地址
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function store(Request $request, Address $address)
    {
        $re = $address->addAddress($request->all());
        if (!$re) return ApiResponse([], $address->error, 1);
        return ApiResponse();
    }

    /**
     * 获取一条地址信息
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function show(Request $request, Address $address)
    {
        $address = $address->getAddress($request->address_id);
        return ApiResponse($address);
    }

    /**
     * 更新地址
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function update(Request $request, Address $address)
    {
        $re = $address->editAddress($request->all());
        if (!$re) return ApiResponse([], $address->error, 1);
        return ApiResponse();
    }

    /**
     * 删除地址
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function delete(Request $request, Address $address)
    {
        $address->delAddress($request->address_id, $request->uid);
        return ApiResponse();
    }

    /**
     * 设置默认收货地址
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function setDefault(Request $request, Address $address)
    {
        $re = $address->editAddress(['address_id' => $request->address_id, 'uid' => $request->uid, 'status' => 1]);
        if (!$re) return ApiResponse([], $address->error, 1);
        return ApiResponse();
    }

    /**
     * 获取默认收货地址
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function getDefault(Request $request, Address $address)
    {
        $address = $address->getDefaultAddress($request->uid);
        return ApiResponse($address);
    }
}
